package com.yuexunit.operationman.nbsy.module.OperationInventory.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.NewGoodsBean;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by chenss on 2017/8/15 0015.
 */

public class AddInventoryAdapter extends KJAdapter<NewGoodsBean.ObjectEntity> implements Filterable {
    private PrescriptionFilter filter;
    ArrayList<NewGoodsBean.ObjectEntity> list = new ArrayList<>();
    ArrayList<NewGoodsBean.ObjectEntity> list_filter = new ArrayList<>();
//    HashMap<Integer, String> saveMap;//这个集合用来存储对应位置上Editext中的文本内容
    public AddInventoryAdapter(AbsListView view, Collection<NewGoodsBean.ObjectEntity> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        list = (ArrayList<NewGoodsBean.ObjectEntity>)((ArrayList)mDatas).clone();
        list_filter = (ArrayList<NewGoodsBean.ObjectEntity>)((ArrayList)mDatas).clone();
    }

    public ArrayList<NewGoodsBean.ObjectEntity> getGoodsValue() {
        return list;
    }

    @Override
    public void convert(AdapterHolder helper, final NewGoodsBean.ObjectEntity item, boolean isScrolling, final int position) {
        if(list_filter.get(position).ishow())
        {
            helper.getView(R.id.add_visibale).setVisibility(View.VISIBLE);
            helper.getView(R.id.add_invisibale).setVisibility(View.GONE);
        }
        else {
            helper.getView(R.id.add_visibale).setVisibility(View.GONE);
            helper.getView(R.id.add_invisibale).setVisibility(View.VISIBLE);
        }
        helper.setText(R.id.name, item.getItemName() + "(" + item.getItemCode() + ")");
        final ImageView pic;
        pic = helper.getView(R.id.pic);
        final EditText num = helper.getView(R.id.number);

        if(list.get(position).getType() == 0){
            pic.setImageResource(R.drawable.icon_inventory_add);
            num.setVisibility(View.GONE);
        }else{
            pic.setImageResource(R.drawable.icon_inventory_subtract);
            num.setVisibility(View.VISIBLE);
        }


        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.get(position).getType() == 0) {
                    pic.setImageResource(R.drawable.icon_inventory_subtract);
                    num.setVisibility(View.VISIBLE);
                    list.get(position).setType(1);
                } else {
                    pic.setImageResource(R.drawable.icon_inventory_add);
                    num.setVisibility(View.GONE);
                    list.get(position).setType(0);
                }
            }
        });
        NewGoodsBean.ObjectEntity objectEntity = list.get(position);
        num.setTag(position);
        num.clearFocus();
        if(!TextUtils.isEmpty(objectEntity.getCount()))
        {
            num.setText(objectEntity.getCount());
        }
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Integer tag= (Integer) num.getTag();
                list.get(tag).setCount(s.toString());
            }
        });
    }


    @Override
    public Filter getFilter() {
        if(filter == null){
            filter = new PrescriptionFilter();
        }
        return filter;
    }

    class PrescriptionFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(constraint == null){
                results.values = list;
                results.count = list.size();
            }else{
                list_filter.clear();
                for (NewGoodsBean.ObjectEntity p : list){
                    if(constraint.toString().length() == 0)
                    {
                        p.setIshow(true);
                        list_filter.add(p);
                    }
                    else {
                        if (p.getItemName().contains(constraint.toString()) || p.getItemCode().contains(constraint.toString())) {
                            p.setIshow(true);
                            list_filter.add(p);
                        } else {
                            p.setIshow(false);
                            list_filter.add(p);
                        }
                    }
                }
                results.values = list_filter;
                results.count = list_filter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            refresh((Collection<NewGoodsBean.ObjectEntity>) results.values);
        }
    }
}
